package com.qiantu.phone.ui.shotview;

import android.content.Context;
import android.view.View;

import androidx.lifecycle.LifecycleOwner;

import com.hjq.http.listener.HttpCallback;
import com.qiantu.api.db.LLDeviceDBManager;
import com.qiantu.api.entity.DeviceBean;
import com.qiantu.api.entity.DeviceCustomKeyBean;
import com.qiantu.api.http.LLHttpManager;
import com.qiantu.api.http.model.HttpData;
import com.qiantu.phone.R;
import com.qiantu.phone.app.AppActivity;
import com.qiantu.phone.ui.uiutil.LLDeviceUtil;
import com.qiantu.phone.widget.btnview.OneButtonView;
import com.qiantu.phone.widget.btnview.OnBtnClickListener;
import com.qiantu.phone.widget.btnview.OneTwoThreeFourSixEightNineButtonView;

import java.util.List;

import okhttp3.Call;

/**
 * @ClassName DeviceLightView
 * @Description TODO
 * @Author ShiJunqiang
 * @Date 2021/12/8 23:05
 * @Version 1.0
 */
public class TenKeyShotView extends KeyDeviceActView {

    private OneButtonView btnView1;
    private OneTwoThreeFourSixEightNineButtonView btnView;
    private String key;
    private DeviceBean mDevice;

    public TenKeyShotView(Context context, String deviceType) {
        super(context, deviceType);
    }

    public void setDevice(DeviceBean device){
        mDevice = device;
    }

    @Override
    public int getLayoutId() {
        return R.layout.ten_key_shot_view;
    }

    @Override
    public void initView() {
        btnView = findViewById(R.id.btn_view);
        btnView1 = findViewById(R.id.btn_view1);
        if(deviceType.contains("wireless_433")){
            key = LLDeviceUtil.WIRELESS_CTRL_433;
        }else if(deviceType.contains("wireless_315")){
            key = LLDeviceUtil.WIRELESS_CTRL_315;
        }else if(deviceType.contains("infra-red")){
            key = LLDeviceUtil.IR_CTRL;
        }
        btnView1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addAct(key, "1");
            }
        });
        btnView.setOnBtnClickListener(new OnBtnClickListener() {
            @Override
            public void onClick(int btnID) {
                int value = btnID + 1;
                if(mDevice != null){
                    addAct(key, String.valueOf(value), mDevice.getDeviceSerialNo());
                }else{
                    addAct(key, String.valueOf(value));
                }
            }
        });
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if(mDevice != null){
            updateCustomKey(mDevice.getDeviceCustomKeys());
            getDeviceCustomKeyList();
        }
    }

    private void getDeviceCustomKeyList() {
//        LLHttpManager.getDeviceCustomKeyList((LifecycleOwner) getContext(), mDevice.getDeviceSerialNo(), new HttpCallback<HttpData<DeviceCustomKeyBean>>(null) {
//            @Override
//            public void onStart(Call call) {
//
//            }
//
//            @Override
//            public void onSucceed(HttpData<DeviceCustomKeyBean> result) {
//                List<DeviceCustomKeyBean.KeyData> customKeyList = result.getData().getDeviceCustomKeys();
//                updateCustomKey(customKeyList);
//                mDevice.setDeviceCustomKeys(customKeyList);
//                LLDeviceDBManager.getInstance(getContext()).put(mDevice);
//            }
//
//            @Override
//            public void onFail(Exception e) {
//
//            }
//
//            @Override
//            public void onEnd(Call call) {
//                ((AppActivity) getContext()).hideDialog();
//            }
//        });
    }

    private void updateCustomKey(List<DeviceCustomKeyBean> customKeyList) {
        if(customKeyList == null){
            return;
        }
        String defaultName = getResources().getString(R.string.custom_name);
        String key1 = defaultName;
        String key2 = defaultName;
        String key3 = defaultName;
        String key4 = defaultName;
        String key5 = defaultName;
        String key6 = defaultName;
        String key7 = defaultName;
        String key8 = defaultName;
        String key9 = defaultName;
        String key10 = defaultName;
        if (customKeyList != null) {
            for (DeviceCustomKeyBean keyData : customKeyList) {
                switch (keyData.getKeyIndex()) {
                    case "1":
                        key1 = keyData.getKeyName();
                        break;
                    case "2":
                        key2 = keyData.getKeyName();
                        break;
                    case "3":
                        key3 = keyData.getKeyName();
                        break;
                    case "4":
                        key4 = keyData.getKeyName();
                        break;
                    case "5":
                        key5 = keyData.getKeyName();
                        break;
                    case "6":
                        key6 = keyData.getKeyName();
                        break;
                    case "7":
                        key7 = keyData.getKeyName();
                        break;
                    case "8":
                        key8 = keyData.getKeyName();
                        break;
                    case "9":
                        key9 = keyData.getKeyName();
                        break;
                    case "10":
                        key10 = keyData.getKeyName();
                        break;
                }
            }
        }
        btnView.setText(key2, key3, key4, key5, key6, key7, key8, key9, key10);
    }

    @Override
    public void updateView() {
    }
}
